import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * Write a description of class Tank here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Tank extends Enemy
{
    GreenfootImage tank = new GreenfootImage(60,20);
    GreenfootImage buffer = new GreenfootImage(30,20);
    boolean active=false,retreating=false;
    Zony z;
    int a=0,warmup=0,health=40,immune=9;
    
    public Tank(){
        setImage(buffer);
    }
    
    protected void addedToWorld(World world)
    {
        tank=((it)getWorld()).getEn1();  
        Zony it = (Zony) getWorld().getObjects(Zony.class).get(0);
        z=it;
    }   
    
    public void act() 
    {
        if(immune>0)immune--;
        buffer.clear();
        if(getX()%4<2)
            buffer.drawImage(tank,0,0);
        else
            buffer.drawImage(tank,-30,0);
       setLocation(getX()+a,getY());
       
       if(warmup<0 && !retreating && active && Math.abs(z.getX()-getX())<120 && Math.abs(z.getY()-getY())<10){
           a=1;
           if(getX()>z.getX())a=-1;
           warmup=30;
        }else if(Math.abs(z.getX()-getX())>120)retreating = false;
                  
       if(active && (!((it)getWorld()).ok(getX()+16*a,getY()) || ((it)getWorld()).ok(getX()+16*a,getY()+11))){
              a=-a;
              warmup=0;
              retreating=true;
            }
       
        warmup--;
        if(warmup==1){
            Greenfoot.playSound("shot.wav");
            getWorld().addObject(new projectile(1,8*a,-2,true),getX()+17*a,getY()-4);
        }
        
        if(!active && Math.abs(z.getX()-getX())<170 && Math.abs(z.getY()-getY())<10){
            active=true;
            a=-1;
            if(z.getX()>getX())a=1; 
            warmup=60;
        }
        
        if(a==-1)
            buffer.mirrorHorizontally();
           
    }   
    
    public void hurt(int h){   
        buffer.clear();
        if(immune==0){
            health--;
            immune=2;
        }
        retreating=false;
        active=true;
        a=-1;
        if(z.getX()>getX())a=1; 
        if(warmup<0)warmup=20;
        if(health==0)explode();
    }
    
    public void explode(){
        double ang,amp;
        Greenfoot.playSound("explosion.wav");
        for(int f=0;f<30;f++){
            ang = 3.14159*Math.random();
            amp = 2.5*(Math.random()+1);
            getWorld().addObject(new projectile(7,amp*Math.cos(ang),-amp*Math.sin(ang),true),getX()-15+Greenfoot.getRandomNumber(30),getY()-10+Greenfoot.getRandomNumber(20));
        }
        for(int f=0;f<20;f++){
            ang = 3.14159*Math.random();
            amp = 2.5*(Math.random()+1);
            getWorld().addObject(new projectile(Greenfoot.getRandomNumber(3)+2,amp*Math.cos(ang),-amp*Math.sin(ang),true),getX()-15+Greenfoot.getRandomNumber(30),getY()-10+Greenfoot.getRandomNumber(20));
        }
        getWorld().removeObject(this);
    }
    public boolean isDeadly(){
        return true;
    }
    
    
    
}
